//
//  zhnAbhJLCFHomeNoticeCellFeiqWd.m
//  Jlcf_ios
//
//  Created by 邓伟浩 on 2019/4/19.
//  Copyright © 2019 ski. All rights reserved.
//

#import "zhnAbhJLCFHomeNoticeCellFeiqWd.h"
#import "slMLEIJLCFNoticeTextCellybzByE.h"
#import "ZTGCDTimerManager.h"
#import "JLCFBaseWebViewController.h"

@interface zhnAbhJLCFHomeNoticeCellFeiqWd ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *vUlKyLcontentTableViewkMtadz;
@property (nonatomic, assign) int showIndex;

@end

@implementation zhnAbhJLCFHomeNoticeCellFeiqWd

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setUpUI];
    }
    return self;
}

- (void)setUpUI {
    self.contentView.backgroundColor = [UIColor whiteColor];
    
    UITableView *vUlKyLcontentTableViewkMtadz = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    [self.contentView addSubview:vUlKyLcontentTableViewkMtadz];
    vUlKyLcontentTableViewkMtadz.delegate = self;
    vUlKyLcontentTableViewkMtadz.dataSource = self;
    vUlKyLcontentTableViewkMtadz.separatorStyle = UITableViewCellSeparatorStyleNone;
    vUlKyLcontentTableViewkMtadz.showsVerticalScrollIndicator = NO;
    vUlKyLcontentTableViewkMtadz.scrollEnabled = NO;
    [vUlKyLcontentTableViewkMtadz mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.contentView);
    }];
    _vUlKyLcontentTableViewkMtadz = vUlKyLcontentTableViewkMtadz;
    
    UIImageView *noticeImageView = [[UIImageView alloc] init];
    noticeImageView.contentMode = UIViewContentModeScaleAspectFit;
    noticeImageView.image = [UIImage imageNamed:@"icon_home_notice"];
    [self.contentView addSubview:noticeImageView];
    [noticeImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(15);
        make.left.mas_equalTo(self.contentView.mas_left).offset(20);
        make.centerY.mas_equalTo(self.contentView);
    }];
    
    UIImageView *toDetailImageView = [[UIImageView alloc] init];
    toDetailImageView.contentMode = UIViewContentModeScaleAspectFit;
    toDetailImageView.image = [UIImage imageNamed:@"icon_home_toDetail"];
    [self.contentView addSubview:toDetailImageView];
    [toDetailImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(15);
        make.right.mas_equalTo(self.contentView.mas_right).offset(-15);
        make.centerY.mas_equalTo(self.contentView);
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.JRuIeDnoticeArrayQGgqZz.count*2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    slMLEIJLCFNoticeTextCellybzByE *cell = [slMLEIJLCFNoticeTextCellybzByE dequeueReusableCellForTableView:tableView];
    JOLrKXJLCFHomeNoticeModelPDyZEb *noticeModel = self.JRuIeDnoticeArrayQGgqZz[indexPath.row % self.JRuIeDnoticeArrayQGgqZz.count];
    cell.uBShqXnoticeLabelBqMXlb.text = [NSString stringWithFormat:@"%@", noticeModel.topic] ;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 30;
}

- (void)setJRuIeDnoticeArrayQGgqZz:(NSArray<JOLrKXJLCFHomeNoticeModelPDyZEb> *)JRuIeDnoticeArrayQGgqZz {
    _JRuIeDnoticeArrayQGgqZz = JRuIeDnoticeArrayQGgqZz;
    
    [_vUlKyLcontentTableViewkMtadz reloadData];
    
    BIWeakObj(self);
    [[ZTGCDTimerManager sharedInstance] scheduleGCDTimerWithName:@"com.wh.notice" interval:4 queue:dispatch_get_main_queue() repeats:YES option:CancelPreviousTimerAction action:^{
        [selfWeak scroll];
    }];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    JOLrKXJLCFHomeNoticeModelPDyZEb *noticeModel = self.JRuIeDnoticeArrayQGgqZz[indexPath.row % self.JRuIeDnoticeArrayQGgqZz.count];
    JLCFBaseWebViewController *webVC = [[JLCFBaseWebViewController alloc] init];
    webVC.url = noticeModel.msg;
    [[[JinLiCaiFuApplicationEntrace shareEntrance] currentNavController] pushViewController:webVC animated:YES];
}

- (void)scroll {
    if (self.JRuIeDnoticeArrayQGgqZz.count != 0) {
        if (_showIndex == self.JRuIeDnoticeArrayQGgqZz.count) {
            [self.vUlKyLcontentTableViewkMtadz scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:NO];
            _showIndex = 0;
        }
        
        _showIndex++;
        [self.vUlKyLcontentTableViewkMtadz scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:_showIndex inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
    }
    
}

@end
